<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="产品编号" prop="ycNumber">
        <el-input
          v-model="queryParams.productId"
          placeholder="产品编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="ycNumber">
        <el-input
          v-model="queryParams.productName"
          placeholder="产品名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column width="200" label="产品编号" align="center" prop="productId"/>
      <el-table-column width="150" label="产品名称" align="center" prop="product">
        <template slot-scope="scope">
          {{scope.row.productDto.productName}}
        </template>
      </el-table-column>
      <el-table-column width="110" label="产品区域" align="center" prop="mobileTelNum"/>
      <el-table-column width="110" label="出发地" align="center" prop="mobileTelNum"/>
      <el-table-column width="110" label="目的地" align="center" prop="mobileTelNum"/>
      <el-table-column width="110" label="出发班期" align="center" prop="leaveFlight"/>
      <el-table-column width="110" label="出发日期" align="center" prop="firstLeaveDt"/>
      <el-table-column width="110" label="行程天数" align="center" prop="travelDay"/>
      <el-table-column width="110" label="最低价" align="center" prop="minPrice"/>
      <el-table-column width="110" label="产品形态" align="center" prop="productShapeInd">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.PRODUCT_SHAPE_IND" :value="scope.row.productShapeInd"/>
        </template>
      </el-table-column>
      <el-table-column width="" label="销售状态" align="center" prop="activeInd">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.PRODUCT_ACTIVE_IND" :value="scope.row.productDto.activeInd"/>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" width="200" label="附件" align="left" prop="fileUrls">
        <template slot-scope="scope">
          <div v-for="fileUrl in scope.row.fileList" style="color: #00afff" @click="handleClick(fileUrl)">
            {{ fileUrl!= null ? fileUrl: '' }}
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column width="" :show-overflow-tooltip="true" label="附件地址" align="center" prop="fileUrls"/>-->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="260">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSet(scope.row)"
          >设置销售计划
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="createFileQuery(scope.row)"
          >生成附件
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bournDetail(scope.row)"
          >地接社
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bournLog(scope.row)"
          >地接社记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Detail ref="detailDialog"/>
    <Create ref="createDialog"/>
    <Bourn ref="bournDia"/>
    <BournLog ref="bournLogDia"/>
  </div>
</template>

<script>
import {
  cancelContract
} from "@/api/salepurchase/contract"
import {
  updateFiles
} from "@/api/salepurchase/tourproduct"
import {pageList,travelPlanList,getById} from "@/api/product/tourproduct"
import Create from "@/views/product/tourproduct/create";
import Detail from "@/views/salepurchase/contract/planDetail/index";
import BournLog from "@/views/product/bourn/indexlog";
import Bourn from "@/views/product/bourn/index";
import {getContractHtml, getPlanHtml, htmlKey1, htmlKey2} from "@/utils/getContractHtml";
import {htmlMakePdf} from "@/utils/htmlMakePdf";
export default {
  name: "yccontract",
  components: {Detail,Create,Bourn,BournLog},
  dicts: ['PRODUCT_SHAPE_IND', 'PRODUCT_ACTIVE_IND'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: "",
        productName: "",
      },
    }
  },
  created() {
    this.getList()
  },
  computed: {
    fileSplit(files) {
      return files.split(',');
    }
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true
      pageList(this.queryParams).then(response => {
          this.tableData = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除' + row.contractId + '？').then(function() {
        return cancelContract(row.id);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    handleUpdate(row) {
      this.$refs.createDialog.open(row.id)
    },
    handleSet(row) {
      this.$modal.confirm('是否确认删除' + row.contractId + '？').then(function() {
        return cancelContract(row.id);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 重置按钮操作 */
    createFileQuery(row) {
      this.createFile(row)
    },
    /** 生成附件 */
    async createFile(row) {
      this.loading=true
      let planHtml = ""
      await travelPlanList({productId:row.productId}).then(res => {
        planHtml = getPlanHtml(res.rows).replaceAll("&#9655;","")
      })
      let contractInfo = {}
      await getById(row.productId).then(res => {
        contractInfo = res.data
      })
      let files = []
      await Promise.all([
        htmlMakePdf(planHtml),
        htmlMakePdf(getContractHtml(contractInfo, htmlKey1)),
        //增加目的地地接社前端信息
        htmlMakePdf(getContractHtml(contractInfo, htmlKey2))
      ]).then((res) => {
        // 结果数组
        files = res.map(i => i.previewUrl)
      })
      await updateFiles({
        productId: row.productId,
        fileList: files
      }).then((response) => {
        this.handleQuery()
        this.loading=false
        alert("成功")
      })
    },
    handleClick(hrefUrl) {
      window.open(hrefUrl, '_blank');
    },
    /** 查看详情操作 */
    handleDetail(row) {
      this.$refs.detailDialog.open(row.productId)
    },
    bournDetail(row) {
      this.$refs.bournDia.open(row.productId)
    },
    bournLog(row) {
      this.$refs.bournLogDia.open(row.productId)
    },
  }
}
</script>
